package com.woniu40.petai_manager.web;

import com.woniu40.petai_manager.entity.ProductType;
import com.woniu40.petai_manager.result.ResponseResult;
import com.woniu40.petai_manager.service.ProductTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("productType")
@Api("一级分类标签")
@CrossOrigin
public class ProductTypeController {

    @Autowired
    ProductTypeService productTypeService;

    @PostMapping("/add")
    @ApiOperation("新增一级分类")
    public ResponseResult<String> add(@RequestBody ProductType productType){
        ResponseResult<String> result = new ResponseResult<>();
        int i = productTypeService.addPro(productType);
        result.setCode(200);
        result.setMessage("添加一级分类成功！");
        return result;
    }


    @GetMapping("/findById")
    @ApiOperation("根据ID查询一级分类对象")
    public ResponseResult<ProductType> toModify(Integer productTypeId){
        ResponseResult<ProductType> result = new ResponseResult<>();
        ProductType productType = productTypeService.findById(productTypeId);
        result.setCode(200);
        result.setMessage("查询一级分类成功！");
        result.setData(productType);
        return result;
    }

    @PutMapping("/modify")
    @ApiOperation("更新一级分类信息")
    public ResponseResult<String> modify(@RequestBody ProductType productType){
        ResponseResult<String> result = new ResponseResult<>();
        int i = productTypeService.modifyPro(productType);
        result.setCode(200);
        result.setMessage("更新成功！");
        return result;
    }

    @DeleteMapping("/removeAll")
    @ApiOperation("根据id删除一级分类(可批量)")
    public ResponseResult<String> removeGoods(String ids){
        ResponseResult<String> result = new ResponseResult<>();
        if(ids.contains(",")){
            List<Integer> idList = new ArrayList<>();
            String[] idsStr = ids.split(",");
            for (String idStr : idsStr) {
                idList.add(Integer.valueOf(idStr));
            }
            int i = productTypeService.removeByIds(idList);
        }else{
            Integer id = Integer.valueOf(ids);
            int i = productTypeService.removePro(id);
        }
        result.setCode(200);
        result.setMessage("删除一级分类成功！");
        return result;
    }
}
